<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="推荐类型管理"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="500px"
    >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">

            <el-form-item label="推荐类型名称" prop="recommendTypeName">
                <el-input v-model="formData.recommendTypeName" placeholder="请输入"/>
            </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false" style="margin-right: 20px;">取 消</el-button>
            <el-popconfirm title="确定保存吗？" @onConfirm="submitForm()">
                <el-button slot="reference" type="primary">保存</el-button>
            </el-popconfirm>
        </div>
    </el-dialog>
</template>

<script>
import {ArticleRecommendTypeApi} from '@/api/cms/ArticleRecommendTypeApi'

export default {
    name: 'CmsArticleRecommendTypeUpdate',
    components: {},
    data() {
        return {
            formData: {
                id: '',
                recommendTypeName: '',
            },
            rules: {

                recommendTypeName: [
                    {required: true, message: '请输入推荐类型名称', trigger: 'blur'},
                    {min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur'}
                ],


            },
            dialogVisible: false
        }
    },
    created() {
    },
    methods: {
        // 显示组件
        async showUpdate(id) {
            const that = this

            that.dialogVisible = true
            if (that.$refs['formRef'] !== undefined) {
                that.$refs['formRef'].resetFields()
            }


            that.$nextTick(() => {
                if (id) {
                    ArticleRecommendTypeApi.getInfo(id).then((response) => {
                        that.formData = Object.assign({}, response)
                    })
                }
            })
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (!valid) return false

                if (that.formData.id) {
                    ArticleRecommendTypeApi.edit(that.formData).then(() => {
                        that.dialogVisible = false
                        that.$emit('refresh')
                    })
                } else {
                    ArticleRecommendTypeApi.add(that.formData).then(() => {
                        that.dialogVisible = false
                        that.$emit('refresh')
                    })
                }
            })
        }
    }
}
</script>


